-----------------------------------------------------------------------------------------
-- filename : pion.lua
-- description : Dice script and logic must be here
-----------------------------------------------------------------------------------------

-- load needed libaries/scripts

local layer = require "layer"
local tile = require "tile"
local math = require "math"
local storyboard = require "storyboard"


-- local variables
local pion = {}
local pion_mt = { __index = pion }  --metatable

-- global variables
pion.UP = 0
pion.DOWN = 1
pion.LEFT = 2
pion.RIGHT = 3
pion.cPion = nil


local function normalizeScore(score) 
	if score > 6 then
		score = 6
	elseif score <= 0 then
		score = 1
	end
	return score
end

-- PUBLIC FUNCTION

function pion.new(player,board, rowPos, colPos)
	
	local newPion = {
		board = board or {},
		row = rowPos or 1,
		col = colPos or 1,
		score = 1,
		enable = true,
		player = player or nil,
		image = display.newImageRect( "assets/player-"..player.id.."/pion1.png",43, 51, true ),
		isFloat = false
	}

	layer.insertImageByIndex(layer.PION, newPion.row, newPion.image)

	newPion.image:addEventListener("touch", newPion)
	
	setmetatable( newPion, pion_mt )
	
	board:addPion(newPion)

	--newPion:commit();

	newPion.image:setReferencePoint(display.CenterReferencePoint)
	local x = newPion.board.x + (newPion.col-1)*newPion.board.tileWidth + newPion.board.tileWidth/2
	local y = newPion.board.y + (newPion.row-1)*newPion.board.tileHeight + newPion.board.tileHeight/2 - 4

	local complete = function ( event )
		newPion:setFloat(false)
	end

	transition.to(newPion.image, {time = 650, x = x, y = y, alpha = 1, transition = easing.inOutExpo, onComplete=complete})

	return newPion
end

--

function pion:setScore(newScore)
	self.score = newScore
	self.score = normalizeScore(self.score)
	self:setScoreImage()
end

function pion:addScore(difScore)
	self.score = self.score + difScore
	self.score = normalizeScore(self.score)
end

function pion:mulScore(factor)
	if (self.score <= 3) then
		self.score = self.score * factor
		self.score = normalizeScore(self.score)
	end
end

function pion:divScore(factor)
	self.score = math.ceil(self.score / factor)
	self.score = normalizeScore(self.score)
end

function pion:touch( event )
	if (event.phase == "began" and self.enable and self.isFloat == false) then
		print(self.isFloat)
		if(pion.cPion ~= nil) then
			pion.cPion:setFloat(false)
		end
		layer.clean(layer.DIR)
		self:setFloat(true)
		pion.cPion = self
		local res = false
		res = self:addDirectionPointer(0,1) or res
		res = self:addDirectionPointer(0,-1) or res
		res = self:addDirectionPointer(1,0) or res
    	res = self:addDirectionPointer(-1,0) or res

    	print("remain : " .. self.player:remainPions())
    	if(res == false or self:isMovable() == false) then
    		self:setFloat(false)
    		layer.clean(layer.DIR)
    		if(self.player:remainPions() <= 1) then
	    		self:setEnable(false)
	    		if(self.player:isFinishTurn()) then
	    			local cPlayer = self.player
	    			self.player:nextTurn(cPlayer)
	    		end
    		end
    	end
    elseif (event.phase == "began") then
		if(pion.cPion ~= nil) then
			pion.cPion:setFloat(false)
		end
		layer.clean(layer.DIR)
	end
    return true
end

function pion:setEnable(enable)
	print("enable")
	print(enable)
	if(enable == self.enable) then
		return
	end

	print("enable continue")
	self.enable = enable

	-- local x = self.image.x
	-- local y = self.image.y

	-- self.image.parent:remove(self.image)
	-- self.image = nil
	

	-- if(enable == false) then
	-- 	self.image = display.newImageRect( "assets/player-"..self.player.id.."/select/pion".. self.score..".png",43,  43, true );
	-- 	self.image.x = x
	-- 	self.image.y = y
		
	-- 	x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
	-- 	y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 

	-- 	transition.to(self.image, {time = 700, x = x, y = y, transition = easing.outInExpo, onComplete=complete})
	-- else
	-- 	self.image = display.newImageRect( "assets/player-"..self.player.id.."/pion".. self.score..".png",43, 51, true );
	-- 	self.image.x = x
	-- 	self.image.y = y-4


	-- 	x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
	-- 	y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 - 4
		
	-- 	transition.to(self.image, {time = 700, x = x, y = y, transition = easing.inOutExpo, onComplete=complete})
	-- end
	-- layer.insertImageByIndex(layer.PION,self.row,self.image)
	-- self.image:addEventListener("touch", self)
end

function pion:isMovable()
	return self:checkDirection(-1,0) or self:checkDirection(1,0) or self:checkDirection(0,-1) or self:checkDirection(0,1)
end

function pion:setFloat(_float,complete)

	if(_float ~= self.isFloat) then
		print("====")
		print(_float)
		print(self.isFloat)
		if(_float) then
			local x = self.image.x
			local y = self.image.y

			self.image.parent:remove(self.image)
			self.image = nil

			self.image = display.newImageRect( "assets/player-"..self.player.id.."/select/pion".. self.score..".png",43,  43, true );
			self.image.x = x
			self.image.y = y
			
			x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
			y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 

			transition.to(self.image, {time = 200, x = x, y = y, transition = easing.outInExpo, onComplete=complete})

		elseif (_float ~= true) then

			local x = self.image.x
			local y = self.image.y

			self.image.parent:remove(self.image)
			self.image = nil

			self.image = display.newImageRect( "assets/player-"..self.player.id.."/pion".. self.score..".png",43, 51, true );
			self.image.x = x
			self.image.y = y-4


			x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
			y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 - 4
			
			transition.to(self.image, {time = 200, x = x, y = y, transition = easing.inOutExpo, onComplete=complete})
			
		end
		layer.insertImageByIndex(layer.PION,self.row,self.image)
		self.image:addEventListener("touch", self)
		self.isFloat = _float
		print(self.isFloat)
	end
end

function pion:checkDirection(difX, difY)
	if(self.col + difX > 0 and self.row + difY > 0 and self.col + difX <= self.board.col and self.row + difY <= self.board.row)then
	    	if(self.board:getTileType(self.row + difY,self.col + difX ) == tile.BARRED) then
	    		self.board:getTile(self.row + difY,self.col + difX):playAnim()
	    		return false
	    	elseif (self.board:getTileType(self.row + difY,self.col + difX ) == tile.JUMP) then
	    		self:addDirectionPointer(difX + difX, difY + difY)
	    	elseif (self.board:getTileType(self.row,self.col) == tile.DIRECTION) then
	    		local tile_temp = self.board:getTile(self.row,self.col )
	    		if (tile.DIR_UP == tile_temp.direction and (difY >= 0)) then
	    			return false
	    		elseif (tile.DIR_DOWN == tile_temp.direction and (difY <= 0)) then
	    			return false
	    		elseif (tile.DIR_RIGHT == tile_temp.direction and (difX <= 0)) then
	    			return false
	    		elseif (tile.DIR_LEFT == tile_temp.direction and (difX >= 0)) then
	    			return false
	    		end
	    	elseif (self.board:getTileType(self.row + difY,self.col + difX ) == tile.DIRECTION) then
	    		local tile_temp = self.board:getTile(self.row + difY,self.col + difX )
	    		if (tile.DIR_UP == tile_temp.direction and (difY >= 0)) then
	    			return false
	    		elseif (tile.DIR_DOWN == tile_temp.direction and (difY <= 0)) then
	    			return false
	    		elseif (tile.DIR_RIGHT == tile_temp.direction and (difX <= 0)) then
	    			return false
	    		elseif (tile.DIR_LEFT == tile_temp.direction and (difX >= 0)) then
	    			return false
	    		end
	    	end

	    	local otherPion = self.board:getPion(self.row+difY,self.col+difX)
	    	if(otherPion ~= nil and self:canPushDir(otherPion,difX,difY) == false) then
	    			return false
	    	end

	    return true
	end
end

function pion:addDirectionPointer(difX,difY)
	if(self:checkDirection(difX,difY))then

	    	local image = nil
	    	local isDifX = 0
	    	local isDifY = 0
	    	
	    	if(difX > 0) then
	    		isDifX = 1
	    	elseif (difX < 0 ) then
	    		isDifX = -1
	    	end

	    	if(difY > 0) then
	    		isDifY = 1
	    	elseif(difY < 0) then
	    		isDifY = -1
	    	end

	    	if(self.board:getTileType(self.row + difY,self.col + difX ) == tile.JUMP) then
	    		image = layer.createImage(layer.DIR, "assets/highlight_midle.png", self.board.x + (self.col-1 + difX) * self.board.tileWidth, self.board.y + (self.row-1 + difY) * self.board.tileHeight,45,45)
		    	image:setReferencePoint(display.CenterReferencePoint)
		    	if(difX < 0) then
		    		image.rotation = 180
		    	elseif(difY < 0) then
		    		image.rotation = 270
		    	elseif(difY > 0) then
		    		image.rotation = 90
		    	end
		    elseif(self.board:getTileType(self.row + difY - 1*isDifY,self.col + difX - 1*isDifX ) == tile.JUMP) then
		    	image = layer.createImage(layer.DIR, "assets/highlight_end.png", self.board.x + (self.col-1 + difX) * self.board.tileWidth, self.board.y + (self.row-1 + difY) * self.board.tileHeight,45,45)
		    	image:setReferencePoint(display.CenterReferencePoint)
		    	if(difX < 0) then
		    		image.rotation = 180
		    	elseif(difY < 0) then
		    		image.rotation = 270
		    	elseif(difY > 0) then
		    		image.rotation = 90
		    	end
	    	else
		    	local decor = layer.createImage(layer.DIR, "assets/highlight_decor.png", self.board.x + (self.col-1) * self.board.tileWidth, self.board.y + (self.row-1) * self.board.tileHeight,45,45)
		    	image = layer.createImage(layer.DIR, "assets/highlight.png", self.board.x + (self.col-1 + difX) * self.board.tileWidth, self.board.y + (self.row-1 + difY) * self.board.tileHeight,45,45)
		    	decor:setReferencePoint(display.CenterReferencePoint)
		    	image:setReferencePoint(display.CenterReferencePoint)
		    	if(difX > 0) then
		    		decor.rotation = 180
		    		image.rotation = 180
		    	elseif(difY > 0) then
		    		decor.rotation = 270
		    		image.rotation = 270
		    	elseif(difY < 0) then
		    		decor.rotation = 90
		    		image.rotation = 90
		    	end
	   		end

	    	image.touch = function (event)
	    		local otherPion = self.board:getPion(self.row+difY,self.col+difX)
	    		if(otherPion ~= nil and self:pushDir(otherPion,difX,difY)) then
	    			print("PUSH")
	    			layer.clean(layer.DIR)
	    			self:setFloat(false)
	    			self.enable = false
	    			return true
	    		elseif (otherPion ~= nil) then
	    			print("Failed to PUSH")
	    			layer.clean(layer.DIR)
	    			self:setFloat(false)
	    			return true
	    		end
	    		print("Next Pion is NIL")
	    		print("difX "..difX)
	    		print("difY "..difY)
	    		print(self.col..","..self.row)
				self.col = self.col+difX
				self.row = self.row+difY
				self:commitAndEffect()
				layer.clean(layer.DIR)
				

	    		if(self.player:isFinishTurn()) then
	    			local cPlayer = self.player
	    			self.player:nextTurn(cPlayer)
	    		end
	    		return true
	    	end
	    	image:addEventListener("touch", image)
	    else
	    	return false
    	end

    	return true

end

function pion:addDirectionPointerAt(row,col)
	
	local otherPion = self.board:getPion(row,col)
	if(otherPion ~= nil) then
		return
	end

	local image = layer.createImage(layer.DIR, "assets/highlight_alone.png", self.board.x + (col-1) * self.board.tileWidth, self.board.y + (row-1) * self.board.tileHeight,45,45)
	image.touch = function (event)
		self.col = col
		self.row = row
		self:commit()
		layer.clean(layer.DIR)
		layer.insertImageByIndex(layer.PION, self.row, self.image)
		self:setEnable(false)

		if(self.player:isFinishTurn()) then
			local cPlayer = self.player
			self.player:nextTurn(cPlayer)
		end
	end

	image:addEventListener("touch", image)
end

function pion:addBeamPointerAt(row,col)
	
	local otherPion = self.board:getPion(row,col)
	if(otherPion ~= nil) then
		return
	end

	local sheetData = {
		width=self.board.tileWidth,
		height=self.board.tileHeight,
		numFrames=4,
		sheetContentWidth=self.board.tileWidth*4,
		sheetContentHeight=self.board.tileHeight*2
	}

	local mySheet = graphics.newImageSheet("assets/tile_animation/beam_sheet.png",sheetData)
	
	local sequenceData = {
		{
			name = "animation",
			start = 1,
			count = 4,
			time = 1000, -- time in millisecond
			loopCount = 0,
			loopDirection = "forward"
		}
	}

	local animation = display.newSprite(mySheet, sequenceData)
	animation:setReferencePoint(display.TopLeftReferencePoint)
	animation.x = self.board.x + (col-1) * self.board.tileWidth
	animation.y = self.board.y + (row-1) * self.board.tileHeight
	animation:setReferencePoint(display.CenterReferencePoint)

	animation:play()
	
	layer.insertImage(layer.DIR,animation)

	local image = animation
	image.touch = function (event)
		transition.to(self.image, {time = 650, y = self.image.y-10, alpha = 0, transition = easing.inOutExpo})
		self.col = col
		self.row = row
		
		-- beam Animation
		local sheetData = {
			width=169,
			height=167,
			numFrames=5,
			sheetContentWidth=169*3,
			sheetContentHeight=167*2
		}

		local mySheet = graphics.newImageSheet("assets/tile_animation/beam_move_sheet.png",sheetData)
		
		local sequenceData = {
			{
				name = "animation",
				start = 1,
				count = 4,
				time = 1000, -- time in millisecond
				loopCount = 1,
				loopDirection = "forward"
			}
		}

		local animation = display.newSprite(mySheet, sequenceData)
		animation:setReferencePoint(display.CenterReferencePoint)
		animation.x = image.x
		animation.y = image.y

		local animationEnded = function ( event )
			if ( event.phase == "ended" ) then
				local thisSprite = event.target --"event.target" references the sprite
				thisSprite.parent:remove(thisSprite)
				self.image:setReferencePoint(display.CenterReferencePoint)
				print(self.col..","..self.row)
				local x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
				local y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 - 4
				self.image.x = x
				self.image.y = y - 20
				self:commit()
				
			end
		end
		animation:addEventListener("sprite", animationEnded)
		animation:play()
		-- end

		layer.clean(layer.DIR)
		layer.insertImageByIndex(layer.PION, self.row, self.image)
		self:setEnable(false)

		if(self.player:isFinishTurn()) then
			local cPlayer = self.player
			self.player:nextTurn(cPlayer)
		end
	end

	image:addEventListener("touch", image)
end

function pion:setEffect(tileType)
	local dead = false
	if(tileType == tile.PLUS1) then
		local sheetData = {
			width=169,
			height=166,
			numFrames=6,
			sheetContentWidth=169*3,
			sheetContentHeight=166*2
		}

		local mySheet = graphics.newImageSheet("assets/tile_animation/plus_sheet.png",sheetData)
		
		local sequenceData = {
			{
				name = "animation",
				start = 1,
				count = 6,
				time = 800, -- time in millisecond
				loopCount = 1,
				loopDirection = "forward"
			}
		}

		local animation = display.newSprite(mySheet, sequenceData)
		animation.x = self.image.x
		animation.y = self.image.y

		local animationEnded = function ( event )
			 if ( event.phase == "ended" ) then
				local thisSprite = event.target --"event.target" references the sprite
				thisSprite.parent:remove(thisSprite)
				self:addScore(1)
				self:setScoreImage()
				self.player:addScore(tile.PLUS1)
				self:setFloat(false)
			end
		end
		animation:addEventListener("sprite", animationEnded)
		animation:play()
	elseif (tileType == tile.MIN1 and self.score > 0) then
		local sheetData = {
			width=169,
			height=166,
			numFrames=6,
			sheetContentWidth=169*3,
			sheetContentHeight=166*2
		}

		local mySheet = graphics.newImageSheet("assets/tile_animation/minus_sheet.png",sheetData)
		
		local sequenceData = {
			{
				name = "animation",
				start = 1,
				count = 6,
				time = 800, -- time in millisecond
				loopCount = 1,
				loopDirection = "forward"
			}
		}

		local animation = display.newSprite(mySheet, sequenceData)
		animation.x = self.image.x
		animation.y = self.image.y

		local animationEnded = function ( event )
			 if ( event.phase == "ended" ) then
				local thisSprite = event.target --"event.target" references the sprite
				thisSprite.parent:remove(thisSprite)
				self:addScore(-1)
				self:setScoreImage()
				self.player:addScore(tile.MIN1)
				self:setFloat(false)
			end
		end
		animation:addEventListener("sprite", animationEnded)
		animation:play()

	elseif (tileType == tile.HALF) then
		local sheetData = {
			width=169,
			height=166,
			numFrames=6,
			sheetContentWidth=169*3,
			sheetContentHeight=166*2
		}

		local mySheet = graphics.newImageSheet("assets/tile_animation/half_sheet.png",sheetData)
		
		local sequenceData = {
			{
				name = "animation",
				start = 1,
				count = 6,
				time = 800, -- time in millisecond
				loopCount = 1,
				loopDirection = "forward"
			}
		}

		local animation = display.newSprite(mySheet, sequenceData)
		animation.x = self.image.x
		animation.y = self.image.y

		local animationEnded = function ( event )
			 if ( event.phase == "ended" ) then
				local thisSprite = event.target --"event.target" references the sprite
				thisSprite.parent:remove(thisSprite)
				self:divScore(2)
				self:setScoreImage()
				self.player:addScore(tile.HALF)
				self:setFloat(false)
			end
		end
		animation:addEventListener("sprite", animationEnded)
		animation:play()
	elseif (tileType == tile.DOUBLE) then
		local sheetData = {
			width=169,
			height=166,
			numFrames=6,
			sheetContentWidth=169*3,
			sheetContentHeight=166*2
		}

		local mySheet = graphics.newImageSheet("assets/tile_animation/double_sheet.png",sheetData)
		
		local sequenceData = {
			{
				name = "animation",
				start = 1,
				count = 6,
				time = 800, -- time in millisecond
				loopCount = 1,
				loopDirection = "forward"
			}
		}

		local animation = display.newSprite(mySheet, sequenceData)
		animation.x = self.image.x
		animation.y = self.image.y

		local animationEnded = function ( event )
			 if ( event.phase == "ended" ) then
				local thisSprite = event.target --"event.target" references the sprite
				thisSprite.parent:remove(thisSprite)
				self:mulScore(2)
				self:setScoreImage()
				self.player:addScore(tile.DOUBLE)
				self:setFloat(false)
			end
		end
		animation:addEventListener("sprite", animationEnded)
		animation:play()
	elseif (tileType == tile.DEATH) then
		print("dead")
		dead = true
		self.player:addScore(tile.DEATH)
		self:commit()
		local deadDelay = function (event)

			-- Death Animation
			local sheetData = {
				width=169,
				height=167,
				numFrames=6,
				sheetContentWidth=169*3,
				sheetContentHeight=167*2
			}

			local mySheet = graphics.newImageSheet("assets/tile_animation/death_sheet.png",sheetData)
			
			local sequenceData = {
				{
					name = "animation",
					start = 1,
					count = 6,
					time = 800, -- time in millisecond
					loopCount = 1,
					loopDirection = "forward"
				}
			}

			local animation = display.newSprite(mySheet, sequenceData)
			animation.x = self.image.x
			animation.y = self.image.y

			local animationEnded = function ( event )
				 if ( event.phase == "ended" ) then
					local thisSprite = event.target --"event.target" references the sprite
					thisSprite.parent:remove(thisSprite)
				end
			end
			animation:addEventListener("sprite", animationEnded)

			self:dead()
			animation:play()
		end
		timer.performWithDelay( 800, deadDelay )

	elseif (tileType == tile.BEAM) then
		self:setFloat(false)
		self.player:addScore(tile.BEAM)
		local options = 
		{
			params = 
			{
				callback = self,
				text = "Do you want to activate BEAM ?",
				isModal = true
			}
		}
		storyboard.showOverlay("messagebox_scene",options)
	elseif (tileType == tile.MOVEMENT1) then
		self:setFloat(false)
		self.player:addScore(tile.MOVEMENT1)
		local options = 
		{
			params = 
			{
				callback = self,
				text = "Do you want to activate MOVEMENT1 ?"
			}
		}
		storyboard.showOverlay("messagebox_scene",options)
	elseif (tileType == tile.REROLL) then
		self:setFloat(false)
		self.player:addScore(tile.REROLL)
		local options = 
		{
			params = 
			{
				callback = self,
				text = "Do you want to activate REROLL ?"
			}
		}
		storyboard.showOverlay("messagebox_scene",options)
	elseif (tileType == tile.REBIRTH) then
		self:setFloat(false)
		self.player:addScore(tile.REBIRTH)
		local options = 
		{
			params = 
			{
				callback = self,
				text = "Do you want to activate REBIRTH ?"
			}
		}
		storyboard.showOverlay("messagebox_scene",options)
	else
		self:setFloat(false)
	end
	if(dead == false) then
		layer.insertImageByIndex(layer.PION, self.row, self.image)
	end
end

function pion:callbackListen(event) 
	if(event.result == 1 and self.board:getTileType(self.row,self.col) == tile.MOVEMENT1) then
		self:addDirectionPointer(0,1)
		self:addDirectionPointer(0,-1)
		self:addDirectionPointer(1,0)
		self:addDirectionPointer(-1,0)
	elseif (event.result == 1 and self.board:getTileType(self.row,self.col) == tile.BEAM) then
		self:beamEffect()
	elseif (event.result == 1 and self.board:getTileType(self.row,self.col) == tile.REROLL) then
		if(event.resultRoll == nil) then
			self.player:addScore(tile.REROLL)
			local options = 
			{
				params = 
				{
					callback = self,
					playerid = self.player.id,
					text = "ROLL"
				}
			}
			storyboard.showOverlay("roll_box",options)
		else
			self:setScore(event.resultRoll)
		end
	elseif (event.result == 1 and self.board:getTileType(self.row,self.col) == tile.REBIRTH) then
		if(event.resultRoll == nil) then
			self.player:addScore(tile.REROLL)
			local options = 
			{
				params = 
				{
					callback = self,
					playerid = self.player.id,
					text = "ROLL"
				}
			}
			storyboard.showOverlay("roll_box",options)
		else
			self:setScore(event.resultRoll)
			if(event.resultRoll == 1) then
				-- REBIRTH one DICE
			end
		end
	end
	layer.insertImageByIndex(layer.PION, self.row, self.image)
end

function pion:beamEffect() 
	for i=1, self.board.row do 
		for j=1, self.board.col do
			if(self.board:getTileType(i,j) == tile.BEAM) then
				self:addBeamPointerAt(i,j)
			end
		end
	end
end

function pion:setEffectPos()
	print(self.row .. "," .. self.col..":"..self.board.map[self.row][self.col].tile_type);
	self:setEffect(self.board.map[self.row][self.col].tile_type)
end

function pion:setScoreImage()
	local x = self.image.x
	local y = self.image.y
	self.image.parent:remove(self.image)
	self.image = nil

	if(self.isFloat) then
		self.image = display.newImageRect( "assets/player-"..self.player.id.."/select/pion".. self.score..".png",43, 51, true );
	else
		self.image = display.newImageRect( "assets/player-"..self.player.id.."/pion".. self.score..".png",43, 51, true );
	end
	self.image.x = x
	self.image.y = y
	self:commit()
	layer.insertImageByIndex(layer.PION,self.row,self.image)
	self.image:addEventListener("touch", self)
	layer.clean(layer.DIR)
end

function pion:push(_pion)
	if(_pion == nil) then 
		return false
	end
	local dirX = self.col - pion.col
	local dirY = self.row - pion.row 

	return true
end

function pion:pushDir(_pion,dirX,dirY)
	if(self.player.id == _pion.player.id) then 
		if(_pion:pushNextScore(self.score+_pion.score,dirX,dirY) == false) then
			return false
		end
	else
		if(self.score < _pion.score) then return false end
		if(_pion:pushMinScore(self.score-_pion.score,dirX,dirY) == false) then
			return false
		end
	end

	if(_pion == nil) then
		print("Because Next Is NIL") 
		return false 
	end
	local isDead = false

	if(_pion.row + dirY < 1 or _pion.row + dirY > self.board.row or _pion.col + dirX < 1 or _pion.col + dirX > self.board.col) then
		isDead = true
	end
	_pion.row = _pion.row + dirY
	_pion.col = _pion.col + dirX
	self.row = self.row + dirY
	self.col = self.col + dirX

	if(isDead) then
		_pion:dead()
	else
		_pion:setEffectPos()
		if(_pion.image ~= nil) then
			_pion:commit()
		end
	end
	self:setEffectPos()
	self:commit()

	return true
end

function pion:canPushDir(_pion,dirX,dirY)
	if(self.player.id == _pion.player.id) then 
		if(_pion:canPushNextScore(self.score+_pion.score,dirX,dirY) == false) then
			return false
		end
	else
		if(self.score < _pion.score) then return false end
		if(_pion:canPushMinScore(self.score-_pion.score,dirX,dirY) == false) then
			return false
		end
	end

	if(_pion == nil) then
		print("Because Next Is NIL") 
		return false 
	end

	return true
end

function pion:canPushNextScore(score,dirX,dirY)
	print("Push Next Score")
	if(self.row + dirY < 1 or self.row + dirY > self.board.row or self.col + dirX < 1 or self.col + dirX > self.board.col) then
		return false
	end

	local tile_type = self.board:getTileType(self.row+dirY,self.col+dirX);
	if(tile_type == tile.BARRED) then
		return false
	elseif (tile_type ==  tile.DIRECTION) then
		local tile_temp = self.board:getTile(self.row + dirY,self.col + dirX )
		if (tile.DIR_UP == tile_temp.direction and (dirY >= 0)) then
			return false
		elseif (tile.DIR_DOWN == tile_temp.direction and (dirY <= 0)) then
			return false
		elseif (tile.DIR_RIGHT == tile_temp.direction and (dirX <= 0)) then
			return false
		elseif (tile.DIR_LEFT == tile_temp.direction and (dirX >= 0)) then
			return false
		end
	end

	local _pion = self.board:getPion(self.row+dirY,self.col+dirX);
	if(_pion == nil) then
		return false
	end

	if(self.player.id == _pion.player.id) then 
		if(_pion:canPushNextScore(score+_pion.score,dirX,dirY) == false) then
			return false
		end
	else
		if(score < _pion.score) then return false end
		if(_pion:canPushMinScore(score-_pion.score,dirX,dirY) == false) then
			return false
		end
	end

	return true
end

function pion:pushNextScore(score,dirX,dirY)
	print("Push Next Score")
	if(self.row + dirY < 1 or self.row + dirY > self.board.row or self.col + dirX < 1 or self.col + dirX > self.board.col) then
		return false
	end

	local tile_type = self.board:getTileType(self.row+dirY,self.col+dirX);
	if(tile_type == tile.BARRED) then
		return false
	elseif (tile_type ==  tile.DIRECTION) then
		local tile_temp = self.board:getTile(self.row + dirY,self.col + dirX )
		if (tile.DIR_UP == tile_temp.direction and (dirY >= 0)) then
			return false
		elseif (tile.DIR_DOWN == tile_temp.direction and (dirY <= 0)) then
			return false
		elseif (tile.DIR_RIGHT == tile_temp.direction and (dirX <= 0)) then
			return false
		elseif (tile.DIR_LEFT == tile_temp.direction and (dirX >= 0)) then
			return false
		end
	end

	local _pion = self.board:getPion(self.row+dirY,self.col+dirX);
	if(_pion == nil) then
		return false
	end

	if(self.player.id == _pion.player.id) then 
		if(_pion:pushNextScore(score+_pion.score,dirX,dirY) == false) then
			return false
		end
	else
		if(score < _pion.score) then return false end
		if(_pion:pushMinScore(score-_pion.score,dirX,dirY) == false) then
			return false
		end
	end

	local isDead = false
	if(_pion.row + dirY < 1 or _pion.row + dirY > self.board.row or _pion.col + dirX < 1 or _pion.col + dirX > self.board.col) then
		isDead = true
	end
	_pion.row = _pion.row + dirY
	_pion.col = _pion.col + dirX

	if(isDead) then
		_pion:dead()
	else
		_pion:setEffectPos()
		if(_pion.image ~= nil) then
			_pion:commit()
		end
	end

	return true
end

function pion:canPushMinScore(score,dirX,dirY)
	if(self.row + dirY < 1 or self.row + dirY > self.board.row or self.col + dirX < 1 or self.col + dirX > self.board.col) then
		return true
	end

	local tile_type = self.board:getTileType(self.row+dirY,self.col+dirX);
	if(tile_type == tile.BARRED) then
		print("Push Min Score Failed because Tile is BARRED")
		return false
	elseif (tile_type ==  tile.DIRECTION) then
		print("Push Min Score Failed because Tile is DIRECTION")
		local tile_temp = self.board:getTile(self.row + dirY,self.col + dirX )
		if (tile.DIR_UP == tile_temp.direction and (dirY >= 0)) then
			return false
		elseif (tile.DIR_DOWN == tile_temp.direction and (dirY <= 0)) then
			return false
		elseif (tile.DIR_RIGHT == tile_temp.direction and (dirX <= 0)) then
			return false
		elseif (tile.DIR_LEFT == tile_temp.direction and (dirX >= 0)) then
			return false
		end
	end

	local _pion = self.board:getPion(self.row+dirY,self.col+dirX);
	if(_pion == nil) then
		return true
	end

	if(score < _pion.score) then 
		return false 
	end

	if(_pion:canPushMinScore(score-_pion.score,dirX,dirY) == false) then
		return false
	end


	return true
end

function pion:pushMinScore(score,dirX,dirY)
	if(self.row + dirY < 1 or self.row + dirY > self.board.row or self.col + dirX < 1 or self.col + dirX > self.board.col) then
		return true
	end

	local tile_type = self.board:getTileType(self.row+dirY,self.col+dirX);
	if(tile_type == tile.BARRED) then
		print("Push Min Score Failed because Tile is BARRED")
		return false
	elseif (tile_type ==  tile.DIRECTION) then
		print("Push Min Score Failed because Tile is DIRECTION")
		local tile_temp = self.board:getTile(self.row + dirY,self.col + dirX )
		if (tile.DIR_UP == tile_temp.direction and (dirY >= 0)) then
			return false
		elseif (tile.DIR_DOWN == tile_temp.direction and (dirY <= 0)) then
			return false
		elseif (tile.DIR_RIGHT == tile_temp.direction and (dirX <= 0)) then
			return false
		elseif (tile.DIR_LEFT == tile_temp.direction and (dirX >= 0)) then
			return false
		end
	end

	local _pion = self.board:getPion(self.row+dirY,self.col+dirX);
	if(_pion == nil) then
		return true
	end

	if(score < _pion.score) then 
		return false 
	end

	if(_pion:pushMinScore(score-_pion.score,dirX,dirY) == false) then
		return false
	end

	local isDead = false
	if(_pion.row + dirY < 1 or _pion.row + dirY > self.board.row or _pion.col + dirX < 1 or _pion.col + dirX > self.board.col) then
		isDead = true
	end
	_pion.row = _pion.row + dirY
	_pion.col = _pion.col + dirX

	if(isDead) then
		_pion:dead()
	else
		_pion:commit()
		_pion:setEffectPos()
	end

	return true
end

function pion:pushNext(dirX,dirY) 
end

function pion:commit()
	self.image:setReferencePoint(display.CenterReferencePoint)
	print(self.col..","..self.row)
	local x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
	local y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 - 4

	local complete = function ( event )
		self:setFloat(false)
	end

	transition.to(self.image, {time = 650, x = x, y = y, alpha = 1, transition = easing.inOutExpo, onComplete=complete})
end

function pion:commitAndEffect()
	self.image:setReferencePoint(display.CenterReferencePoint)
	print("Commit And Effect")
	local x = self.board.x + (self.col-1)*self.board.tileWidth + self.board.tileWidth/2
	local y = self.board.y + (self.row-1)*self.board.tileHeight + self.board.tileHeight/2 - 4

	--local complete = function ( event )

		local complete = function(event)
			self:setEffectPos()
		end
		-- Adding
		--self:setFloat(false,complete2)
		-- end of adding
		
	--end

	transition.to(self.image, {time = 650, x = x, y = y, transition = easing.inOutExpo, onComplete=complete})
end

function pion:dead()
	if(pion.cPion == self) then
		pion.cPion = nil
	end
	self.image.parent:remove(self.image)
	self.image = nil
	self.board:removePion(self)
	self.player:removePion(self)
	self = nil
	-- remove from player
end

return pion